package com.example.demo;

import java.util.concurrent.Future;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.command.AsyncResult;

import rx.Observable;
import rx.Observable.OnSubscribe;
import rx.Subscriber;

@Service
public class SentenceServiceImpl implements SentenceService {

	@Autowired
	SubjectClient subjectClient;
	@Autowired
	ActivityClient activityClient;

	@Override
	@HystrixCommand(fallbackMethod = "getFallbackWord")
	public Future<String> getWord() {
		return new AsyncResult<String>() {
			@Override
			public String invoke() {
				return subjectClient.getWord();
			}
			
		};
	}

//	@Override//勿用，知道HystrixCommand的fallback可以处理异步方法即可
//	@HystrixCommand(fallbackMethod = "getFallbackActivity")
//	public Observable<String> getActivity() {
//		return Observable.create(new OnSubscribe<String>() {
//			@Override
//			public void call(Subscriber<? super String> subscriber) {
//				try {
//					if(subscriber.isUnsubscribed()) {
//						subscriber.onNext(activityClient.getActivity());
//						subscriber.onCompleted();
//					}
//				}catch(Exception e){
//					subscriber.onError(e);
//				}
//			}
//		});
//	}
	@HystrixCommand(fallbackMethod = "getFallbackActivity")
	public String getActivity() {
		return activityClient.getActivity();
	}

	public String getFallbackWord() {
		return "No Word";
	}

	public String getFallbackActivity() {
		return "No Activity";
	}

}
